package com.nisco.dms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Menu;
import com.nisco.dms.entity.Program;
import com.nisco.dms.exception.ServiceException;
import com.nisco.dms.service.MenuService;
import com.nisco.dms.service.ProgramService;
import com.nisco.dms.util.ControllerUtil;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

/**
 * 
 * @author joey
 * 
 */
@Controller
@RequestMapping(value = "/module")
public class ModuleController {
	@Autowired
	private MenuService moduleService;
	@Autowired
	private ProgramService programService;
	private static final String CREATE = "module/moduleCreate";
	private static final String UPDATE = "module/moduleUpdate";
	private static final String LIST = "module/moduleList";
	private static final String TREE = "module/moduleTree";
	private static final String DISTRIBUTEPROGRAM="module/moduleProgram";
	private static String  moduleListNavId = "";
	@RequiresPermissions("Module:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return CREATE;
	}

	@RequiresPermissions("Module:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(Menu module, HttpServletRequest request) {
		module.setParent((Menu) request.getSession().getAttribute("parentModule"));
		moduleService.save(module);
		
		AjaxObject ajaxObject = new AjaxObject("模块添加成功！");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
	}

	@RequiresPermissions("Module:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		Menu menu = moduleService.get(id);

		map.put("menu", menu);
		return UPDATE;
	}

	@RequiresPermissions("Module:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(Menu module) {
		moduleService.update(module);

		AjaxObject ajaxObject = new AjaxObject("模块修改成功！");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
	}

	@RequiresPermissions("Module:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject();
		try {
			moduleService.delete(id);
			ajaxObject.setMessage("模块删除成功！");
		} catch (ServiceException e) {
			ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
			ajaxObject.setMessage("模块删除失败：" + e.getMessage());
		}

		ajaxObject.setCallbackType("");
		ajaxObject.setNavTabId(moduleListNavId);
		ajaxObject.setRel("jbsxBox2module");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Module:view")
	@RequestMapping(value = "/tree",method = { RequestMethod.GET, RequestMethod.POST })
	public String tree(Map<String, Object> map,HttpServletRequest request) {
		moduleListNavId = ControllerUtil.navId(moduleListNavId, (String)request.getParameter("moduleListNavId"));
		Menu menu = moduleService.getTree();

		map.put("menu", menu);
		return TREE;
	}

	@RequiresPermissions("Module:view")
	@RequestMapping(value = "/list/{parentId}", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Page page, @PathVariable String parentId, String keywords, Map<String, Object> map, HttpServletRequest request) {
		List<Menu> modules = null;
		if (StringUtils.isNotBlank(keywords)) {
			modules = moduleService.find(parentId, keywords, page);
		} else {
			modules = moduleService.find(parentId, page);
		}
		Menu menu=moduleService.findOne(parentId);
		boolean isLeafMenu=false;
		if(menu.getParent()!=null)
		if(menu.getParent().getParent()!=null)
		if(menu.getParent().getParent().getStrSn().equals("Root"))
			isLeafMenu=true;
			map.put("isLeafMenu", isLeafMenu);
		if(menu.getProgram()!=null)
			map.put("isProgram", true);
		request.getSession().setAttribute("parentModule", moduleService.get(parentId));

		map.put("page", page);
		map.put("modules", modules);
		map.put("keywords", keywords);
		map.put("parentId", parentId);

		return LIST;
	}
	@RequestMapping(value="/preDistributeProgram/{parentId}")
	public String preDistributeProgram(Page page,Model model,String keywords,@PathVariable(value="parentId")String parentId)
	{
		if(!"".equals(keywords)&&keywords!=null)
			model.addAttribute("programList", programService.findProgramByKeywords(page, keywords));
		else
			model.addAttribute("programList", programService.findAll(page));
		model.addAttribute("page", page);
		model.addAttribute("keywords", keywords);
		model.addAttribute("parentId", parentId);
		return DISTRIBUTEPROGRAM;
	}
	
	@RequestMapping(value="/distributeProgram")
	public @ResponseBody String  distributeProgram(String programId,String menuId)
	{
		Program program=programService.get(programId);
		Menu menu=moduleService.get(menuId);
		Menu newMenu=new Menu();
		newMenu.setParent(menu);
		newMenu.setStrSn(program.getStrSn());
		newMenu.setStrName(program.getStrName());
		newMenu.setStrDesc(program.getStrDesc());
		newMenu.setProgram(program);
		moduleService.save(newMenu);
		AjaxObject ajaxObject = new AjaxObject("程序添加成功！");
		//ajaxObject.setNavTabId("moduleListNav_5");
		ajaxObject.setRel("moduleLayout");
		return ajaxObject.toString();
	}
	
	@RequestMapping(value="/unDistributeProgram")
	public @ResponseBody String  unDistributeProgram(String programId,String fatherId)
	{
		moduleService.deleMenuByInfo(programId, fatherId);
		AjaxObject ajaxObject = new AjaxObject("程序删除成功！");
		//ajaxObject.setNavTabId("moduleListNav_5");
		ajaxObject.setRel("moduleLayout");
		return  ajaxObject.toString();
	}

}
